
package BLogic;


import HibernateLayer.SessionExausedExcepion;
import POJOs.SCRUDBasic;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.zip.*;

public abstract class VZipManager<STRUCT extends SCRUDBasic> {

   int BUFFER = 4096;

   public void decompressZipAndStore(List<STRUCT> users, String storePath, String auxString, String comment){

       try {
         BufferedOutputStream dest = null;
         FileInputStream fis = new FileInputStream(storePath);
         ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
         ZipEntry entry;
         
         Iterator<STRUCT> itera=users.iterator();
         File readFile;

         while((entry = zis.getNextEntry()) != null) {
            System.out.println("VZipManager | decompressZipAndStore | Extracting: " +entry);
            int count;
            byte data[] = new byte[BUFFER];
            
            //Write file into the disk
            //File auxFile=new File(auxString);
            //auxFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(auxString);
            dest = new BufferedOutputStream(fos, BUFFER);

            while ((count = zis.read(data, 0, BUFFER)) != -1) {
               dest.write(data, 0, count);
            }
            
            dest.flush();
            dest.close();

            if(itera.hasNext()){
                readFile=new File(auxString);
                this.dbWriter(itera.next(), entry.getName(), readFile, comment);
            }

         }
         zis.close();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }

   protected abstract void dbWriter(STRUCT user, String fileName, File toWrite, String comm) throws SessionExausedExcepion;


}
